package com.travelexp;

import android.app.ActionBar.LayoutParams;
import android.app.ListActivity;
import android.app.LoaderManager;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SimpleCursorAdapter;

public class OpenTravel extends ListActivity 
	implements LoaderManager.LoaderCallbacks<Cursor>{
	
    private SimpleCursorAdapter mAdapter;	
    private ProgressBar progressBar;
	
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
		// Setup UI
	    super.onCreate(savedInstanceState);
		
	    // Setup the progress bar
	    setupProgressBar();
	    
	    
	    // Columns we want to list
        String[] fromColumns = {TravelData.C_NAME, TravelData.C_ADDINFO};
        int[] toViews = {R.id.row_travel_name, R.id.row_add_info }; 
        
	    // Create an empty adapter we will use to display the loaded data.
        // We pass null for the cursor, then update it in onLoadFinished()
        mAdapter = new SimpleCursorAdapter(this, 
        		   R.layout.open_travel_row, null,
                   fromColumns, toViews, 0);
        		   setListAdapter(mAdapter);
        setListAdapter(mAdapter);
        		   
       // Prepare the loader.  Either re-connect with an existing one, or start a new one.
        getLoaderManager().initLoader(0, null, this);        		   
	}
	
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_open_travel, menu);
        return true;
    }    
	
	@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_cancel:
            	finish();            	
            	return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    } 
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		TravelData td = new TravelData(this);
		td.setCurTravel(id);
		finish();
	}

	public void setupProgressBar()
	{
	    // Create a progress bar to display while the list loads
        progressBar = new ProgressBar(this);
        progressBar.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT, Gravity.CENTER));
        progressBar.setIndeterminate(true);
        getListView().setEmptyView(progressBar);

        // Must add the progress bar to the root of the layout
        ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
        root.addView(progressBar);		
	}
    
	public Loader<Cursor> onCreateLoader(int id, Bundle args) {
		Uri uri = Uri.withAppendedPath(DataProvider.CONTENT_URI, TravelData.T_TRAVEL);
		return new CursorLoader(this, uri, null, null, null, null);
	}

	public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        // Swap the new cursor in.  
        mAdapter.swapCursor(data); 
	}

	public void onLoaderReset(Loader<Cursor> loader) {
		/* This is called when the last Cursor provided to onLoadFinished()
         * above is about to be closed.  We need to make sure we are no
         * longer using it.
         */
        mAdapter.swapCursor(null);	
        
        // Remove the progress bar
        ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
        root.removeView(progressBar);	        
	}

}
